/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.gui.wizard.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;

/**
 * Wizard - Port Chooser Panel. This panel is used to select a port and supports
 * manual or automatic selection of the XBee COM port.
 * 
 * @author Kyle O'Connor
 * 
 */
@SuppressWarnings("serial")
public class PortChooserPanel extends JPanel {

	private final static Logger log = Logger.getLogger(PortChooserPanel.class);

	// gui items
	private JPanel titlePanel;
	private JLabel textLabel;
	private JLabel resultLabel;
	private JLabel portHeadingLabel;
	private JComboBox portDropDownMenu;
	private JButton portDetectButton;
	private JLabel instructionsLabel;
	
	//data model
	protected MainModel model;

	public PortChooserPanel(MainModel aModel) {
		this.model = aModel;
		initComponents();
	}

	private void initComponents() {
		this.setLayout(new BorderLayout());
		
		//title bar
		titlePanel = new JPanel(new BorderLayout());
		titlePanel.setBackground(Color.GRAY);
		textLabel = new JLabel();
		JSeparator separator = new JSeparator();
		textLabel.setBackground(Color.GRAY);
		textLabel.setFont(new Font(textLabel.getFont().getFontName(), Font.BOLD, 16));
		textLabel.setText("Step 1. Choose Port for XBee Device Connection");
		textLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		textLabel.setOpaque(true);
		titlePanel.add(textLabel, BorderLayout.CENTER);
		titlePanel.add(separator, BorderLayout.SOUTH);

		JPanel centerPane = new JPanel(new BorderLayout(0, 10));
		JPanel northPane = new JPanel(new BorderLayout());
		northPane.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
		JPanel northPane2 = new JPanel(new BorderLayout(0, 5));
		JPanel southPane = new JPanel(new BorderLayout(20, 0));

		portHeadingLabel = new JLabel("Available Ports:");
		portDropDownMenu = new JComboBox(model.getPortList());
		resultLabel = new JLabel("Result: ");
		portDetectButton = new JButton("Detect Port");

		northPane.add(portHeadingLabel, BorderLayout.NORTH);
		northPane.add(portDropDownMenu, BorderLayout.SOUTH);
		northPane2.add(northPane, BorderLayout.NORTH);
		southPane.add(portDetectButton, BorderLayout.WEST);
		northPane2.add(southPane, BorderLayout.SOUTH);
		centerPane.add(northPane2, BorderLayout.NORTH);
		centerPane.add(resultLabel, BorderLayout.CENTER);
		add(titlePanel, BorderLayout.NORTH);
		add(centerPane, BorderLayout.CENTER);
		
		instructionsLabel = new JLabel(
				"Select the port for the XBee device and click 'Next' to continue.",
				JLabel.RIGHT);
		add(instructionsLabel, BorderLayout.SOUTH);
	}

	public String getUserInput() {
		return (String) portDropDownMenu.getSelectedItem();
	}
	
	public void setResultText(String resultMsg) {
		// should be called from event dispatch thread
		if (!javax.swing.SwingUtilities.isEventDispatchThread())
			log.warn("Trying to update stuff outside of event dispatch thread!");
		
		resultLabel.setText("Result: " + resultMsg);
	}

	public void setButtonsEnabled(boolean state) {
		// should be called from event dispatch thread
		if (!javax.swing.SwingUtilities.isEventDispatchThread())
			log.warn("Trying to update stuff outside of event dispatch thread!");

		// enable/disable the dropdown and the detect port button
		portDropDownMenu.setEnabled(state);
		portDetectButton.setEnabled(state);
	}

	public void addDetectPortButtonListener(ActionListener al) {
		portDetectButton.addActionListener(al);
	}

}
